#include "foodmenu.h"
#include "ui_foodmenu.h"


QVector <closeinfo> clsinfo;
FoodMenu::FoodMenu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FoodMenu)
{
    ui->setupUi(this);
    initwindow();

}

FoodMenu::~FoodMenu()
{
    delete ui;
}


void FoodMenu::initwindow()
{
    ui->frame_3->setStyleSheet("QPushButton {\
                                       background-color: #87CEFA;\
                                       color: white;\
                                       border: none;\
                                       padding: 10px 10px;\
                                       font-size: 14px;\
                                       border-radius: 6px;\
                                   }\
                                   QPushButton:hover {\
                                       background-color: #778899;\
                                   }\
                                   QPushButton:pressed {\
                                       background-color: #20B2AA\
                                   }"
                                    );
   QSpacerItem *spacer = new QSpacerItem(100,50,QSizePolicy::Minimum,QSizePolicy::Expanding);
   connect(ui->detalframe,&FoodCustom::sendCreatInfo,this,[=](const QString& FoodName,const QString& FoodMoney){

       FoodFrame *foodinfoframe = new FoodFrame(FoodName,FoodMoney);

       ui->frameLayout->insertWidget(0,foodinfoframe);
   });

       ui->frameLayout->addItem(spacer);

}

void FoodMenu::setTabelNum(QString tabelNumber, QString eaternumber)
{
    ui->tablenumber->setText(tabelNumber);
    ui->eaternumber->setText(eaternumber);
}

void FoodMenu::on_pushButton_2_clicked()
{
    emit sendBackSign();
}


FoodFrame::FoodFrame(QString FoodName, QString FoodMoney, QWidget *p)
{
    this->resize(215,335);
    foodNameLabel = new QLabel(FoodName);
    foodNameLabel->resize(100,25);
    foodMoneyLabel = new QLabel(FoodMoney);
    foodMoneyLabel->resize(40,25);
    foodMoneyLabel->setAlignment(Qt::AlignCenter);
    infoHlayout = new QHBoxLayout(this);

    addButton = new QToolButton;
    addButton->setIcon(QIcon(":/icon/add.png"));
    addButton->resize(25,25);
    addButton->setAutoRaise(true);
    delButton = new QToolButton;
    delButton->setIcon(QIcon(":/icon/reduce.png"));
    delButton->resize(25,25);
    delButton->setAutoRaise(true);
    NumLineEdit = new QLineEdit;

    NumLineEdit->setProperty("caiName",FoodName);
    qDebug()<<NumLineEdit->property("caiName").toString();

    NumLineEdit->setFrame(false);
    NumLineEdit->resize(20,20);
    NumLineEdit->setMaximumSize(20,20);
    NumLineEdit->setAlignment(Qt::AlignCenter);
    NumLineEdit->setText("1");

    infoHlayout->addWidget(foodNameLabel);
    infoHlayout->addWidget(foodMoneyLabel)  ;
    infoHlayout->addWidget(addButton);
    infoHlayout->addWidget(NumLineEdit);
    infoHlayout->addWidget(delButton);

    infoHlayout->setSpacing(0);
    infoHlayout->setMargin(0);

    connect(addButton,&QToolButton::clicked,this,[&](){
       int currentNum = this->NumLineEdit->text().toInt();
        if( currentNum >= 1)
           this-> NumLineEdit ->setText(QString::number(currentNum+1));
    });
    connect(delButton,&QToolButton::clicked,this,[&](){
        int currentNum = this->NumLineEdit ->text().toInt();
            if( currentNum >= 1)
            {
                this->NumLineEdit ->setText(QString::number(currentNum-1));
                currentNum = this->NumLineEdit ->text().toInt();
                if(currentNum < 1)
                {
                    this->setHidden(true);

                }
            }
    });


    clsinfo.append(closeinfo(FoodName,FoodMoney,this->NumLineEdit->text()));
    for (QVector<closeinfo>::iterator it = clsinfo.begin(); it != clsinfo.end(); it++) {
            qDebug() << it->name<<it->money<<it->number;
    }
    connect(NumLineEdit,&QLineEdit::textChanged,[&](){
            int i = clsinfo.indexOf(foodNameLabel->text());
            qDebug()<<i;
        qDebug()<<"*****_______********";
        for (QVector<closeinfo>::iterator it = clsinfo.begin(); it != clsinfo.end(); it++) {
                qDebug() << it->name<<it->money<<it->number;
        }

            clsinfo.replace(i,closeinfo{foodNameLabel->text(),foodMoneyLabel->text(), NumLineEdit->text()});
            qDebug()<<"*****";
            for (QVector<closeinfo>::iterator it = clsinfo.begin(); it != clsinfo.end(); it++) {
                    qDebug() << it->name<<it->money<<it->number;
            }
    });


}

ReceiptWindow::ReceiptWindow(QWidget *parent)
{

    QLabel *logolabel = new QLabel;
    QLabel *namelabel = new QLabel("神厨小福贵");
    QFrame* receiptFrame = new QFrame;
    QFrame* mainFrame = new QFrame;
    QHBoxLayout* labelHlayout = new QHBoxLayout;
    QVBoxLayout* receiptVlayout = new QVBoxLayout(mainFrame);
    QVBoxLayout* mainVlayout = new QVBoxLayout(this);
    QPushButton* finalbutton = new QPushButton("结账");

    receiptFrame->setMinimumSize(300,450);
    receiptFrame->setMaximumWidth(300);
    logolabel->setPixmap(QPixmap(":/icon/logo.webp").scaled(90,90));

    labelHlayout->addWidget(logolabel);
    labelHlayout->addWidget(namelabel);

    receiptVlayout->addLayout(labelHlayout);
    receiptVlayout->addWidget(receiptFrame);
    receiptVlayout->addWidget(finalbutton);
    receiptVlayout->setSpacing(0);
    receiptVlayout->setMargin(0);

    mainVlayout->addWidget(mainFrame);
    mainVlayout->setSpacing(0);
    mainVlayout->setMargin(0);

    this->show();


}

void FoodMenu::on_BillingButton_clicked()
{
     ReceiptWindow *receiptwindow = new ReceiptWindow;
    emit closinfo(clsinfo);
}

void FoodMenu::on_toolButton_4_clicked()
{
    int currentNum = ui->eaternumber ->text().toInt();
     if( currentNum >= 1)
        ui->eaternumber ->setText(QString::number(currentNum+1));
}

void FoodMenu::on_toolButton_3_clicked()
{
    int currentNum = ui->eaternumber ->text().toInt();
     if( currentNum > 1)
        ui->eaternumber ->setText(QString::number(currentNum - 1));
}
